<html><head><title>UserGuide - Storing data in memory</title></head>
<body bgcolor="#EFF1F0" link="#3A3966" vlink="#000000" alink="#000000">
<font face="Verdana, sans-serif" size="2"><p align="center"><b><font size="5">UserGuide - Storing data in memory</font></b></p>

This example gathers information about the <a href="../file/index.html">files</a> in the logged on 
user's home directory into a <a href="structures.html">structured</a> 
<a href="newlist.html">list</a>. For now the output isn't very exciting but we 
will come back to this example later on and make it a bit more friendly in several 
different ways. 
<pre><font face="Courier New, Courier, mono"size="2">  <font color="#3A3966">; This section describes the fields of a structure or record, mostly integers in this case,</font>
  <font color="#3A3966">; but notice the string for the file name and the quad for the file size.</font>
  <b><font color="#3A3966">Structure</font></b> FILEITEM
    Name.s
    Attributes.i
    Size.q
    DateCreated.i
    DateAccessed.i
    DateModified.i
  <b><font color="#3A3966">EndStructure</font></b>
  
  <font color="#3A3966">; Now we define a new list of files using the structure previously specified </font>
  <font color="#3A3966">; and some other working variables we'll use later on.</font>
  <b><font color="#3A3966">NewList</font></b> <font color="#3A3966">Files</font>.FILEITEM()
  <b><font color="#3A3966">Define</font></b>.s Folder
  <b><font color="#3A3966">Define</font></b>.l Result
  
  <font color="#3A3966">; This function gets the home directory for the logged on user.</font>
  Folder =<font color="#3A3966"> GetHomeDirectory</font>()
  
  <font color="#3A3966">; Open the directory to enumerate all its contents.</font>
  Result =<font color="#3A3966"> ExamineDirectory</font>(0, Folder, &quot;*.*&quot;)  
  
  <font color="#3A3966">; If this is ok, begin enumeration of entries.</font>
  <b><font color="#3A3966">If</font></b> Result
    <font color="#3A3966">; Loop through until NextDirectoryEntry(0) becomes zero - indicating that there are no more entries.</font>
    <b><font color="#3A3966">While</font></b> <font color="#3A3966">NextDirectoryEntry</font>(0)
      <font color="#3A3966">; If the directory entry is a file, not a folder.</font>
      <b><font color="#3A3966">If</font></b> <font color="#3A3966">DirectoryEntryType</font>(0) = <font color="#924B72">#PB_DirectoryEntry_File</font>
        <font color="#3A3966">; Add a new element to the list.</font>
<font color="#3A3966">        AddElement</font>(<font color="#3A3966">Files</font>())
        <font color="#3A3966">; And populate it with the properties of the file.</font>
<font color="#3A3966">        Files</font>()\Name =<font color="#3A3966"> DirectoryEntryName</font>(0)
<font color="#3A3966">        Files</font>()\Size =<font color="#3A3966"> DirectoryEntrySize</font>(0)
<font color="#3A3966">        Files</font>()\Attributes =<font color="#3A3966"> DirectoryEntryAttributes</font>(0)
<font color="#3A3966">        Files</font>()\DateCreated =<font color="#3A3966"> DirectoryEntryDate</font>(0, <font color="#924B72">#PB_Date_Created</font>)
<font color="#3A3966">        Files</font>()\DateAccessed =<font color="#3A3966"> DirectoryEntryDate</font>(0, <font color="#924B72">#PB_Date_Accessed</font>)
<font color="#3A3966">        Files</font>()\DateModified =<font color="#3A3966"> DirectoryEntryDate</font>(0, <font color="#924B72">#PB_Date_Modified</font>)
      <b><font color="#3A3966">EndIf</font></b>
    <b><font color="#3A3966">Wend</font></b>
    <font color="#3A3966">; Close the directory.</font>
<font color="#3A3966">    FinishDirectory</font>(0)
  <b><font color="#3A3966">EndIf</font></b>
  
  <font color="#3A3966">; Shows the results in the debug window (if there is no entry, nothing will be displayed)</font>
  <b><font color="#3A3966">ForEach</font></b> <font color="#3A3966">Files</font>()
    <b><font color="#3A3966">Debug</font></b> &quot;Filename = &quot; +<font color="#3A3966"> Files</font>()\Name
    <b><font color="#3A3966">Debug</font></b> &quot;Size = &quot; +<font color="#3A3966"> Str</font>(<font color="#3A3966">Files</font>()\Size)
    <b><font color="#3A3966">Debug</font></b> &quot;Attributes = &quot; +<font color="#3A3966"> StrU</font>(<font color="#3A3966">Files</font>()\Attributes)
    <b><font color="#3A3966">Debug</font></b> &quot;Created = &quot; +<font color="#3A3966"> StrU</font>(<font color="#3A3966">Files</font>()\DateCreated)
    <b><font color="#3A3966">Debug</font></b> &quot;Accessed = &quot; +<font color="#3A3966"> StrU</font>(<font color="#3A3966">Files</font>()\DateAccessed)
    <b><font color="#3A3966">Debug</font></b> &quot;Modified = &quot; +<font color="#3A3966"> StrU</font>(<font color="#3A3966">Files</font>()\DateModified)
  <b><font color="#3A3966">Next</font></b> <font color="#3A3966">Files</font>()
</font></pre>

Ok, firstly, the dates in the output are just numbers - this isn't very helpful, 
so let's make them look a bit more familiar. Replace the last three Debug statements 
with these: 
<pre><font face="Courier New, Courier, mono"size="2">      ...
      <b><font color="#3A3966">Debug</font></b> &quot;Created = &quot; +<font color="#3A3966"> FormatDate</font>(&quot;%dd/%mm/%yyyy&quot;,<font color="#3A3966"> Files</font>()\DateCreated)
      <b><font color="#3A3966">Debug</font></b> &quot;Accessed = &quot; +<font color="#3A3966"> FormatDate</font>(&quot;%dd/%mm/%yyyy&quot;,<font color="#3A3966"> Files</font>()\DateAccessed)
      <b><font color="#3A3966">Debug</font></b> &quot;Modified = &quot; +<font color="#3A3966"> FormatDate</font>(&quot;%dd/%mm/%yyyy&quot;,<font color="#3A3966"> Files</font>()\DateModified)
</font></pre>

The <a href="../date/formatdate.html">FormatDate()</a> function takes a date in PureBasic's own numeric date format and 
displays it in a format that we can specify. So now things should begin to look a bit 
more sensible. 
<br>
<br>
Finally, for now, the list isn't in any particular order, so let's 
<a href="../sort/sortstructuredlist.html">sort the list</a> before we display it. Add this 
line before the comment about showing the list and the 
<a href="foreach_next.html">ForEach</a> loop. 
<pre><font face="Courier New, Courier, mono"size="2">  ...
  
  <font color="#3A3966">; Sort the list into ascending alphabetical order of file name.</font>
<font color="#3A3966">  SortStructuredList</font>(<font color="#3A3966">Files</font>(), <font color="#924B72">#PB_Sort_Ascending</font>,<font color="#3A3966"> OffsetOf</font>(FILEITEM\Name), <font color="#924B72">#PB_String</font>)
  
  <font color="#3A3966">; If there are some entries in the list, show the results in the debug window.</font>
  ...
</font></pre>

This command takes the <a href="structures.html">structured</a> <a href="newlist.html">list</a>, 
and resorts it into ascending order (<font color="#924B72">#PB_Sort_Ascending</font>), of the Name field of the structure 
(OffsetOf(FILEITEM\Name)), which is a string value (<font color="#924B72">#PB_String</font>). 


<p><b>UserGuide Navigation</b></p><blockquote>

<a href="ug_string.html">< Previous: String Manipulation</a> | <a href="ug_overview.html">Overview</a> | <a href="ug_inputoutput.html">Next: Input & Output ></a> 
</body></html>